package com.jy.rhin.application.cda;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.dao.SiWangYXZMDao;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.siwang.SiWangYXZMVo;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.body.siwangyxzm.JiaRen;
import com.jy.rhin.model.body.siwangyxzm.SiWangYXZM;
import com.jy.rhin.model.body.siwangyxzm.SiWangYY;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@DS("health")
@Service
public class SiWangYXZMApplication {

    private static final String FTL_PATH = "/11siwangyxzm.ftl";
    @Resource
    private CDACreator cdaCreator;
    /*@Resource
    private DataMigrationAPI dataMigrationAPI;*/
    @Resource
    SiWangYXZMDao siWangYXZMDao;
    @Resource
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String SIWANGZH, String yiliaojgdm) {
        SiWangYXZM siWangYXZM = new SiWangYXZM();
        SiWangYXZMVo vo = siWangYXZMDao.getSiWangYXZMByBM(SIWANGZH, yiliaojgdm);
        if (Objects.isNull(vo)) {
            throw new CDAException("死亡医学证明数据不存在", null);
        }
        tranData(vo);
        siWangYXZM.setEffectiveTime(CDAUtil.toD15(vo.getLURURQ()));
        siWangYXZM.setId(SIWANGZH);
        siWangYXZM.setPatientRole(createPatientRole(vo));
        siWangYXZM.setAuthor(createAuthor(vo));
        siWangYXZM.setCustodian(createCustodian(hospitalAPI.info(vo.getYILIAOJGDM()).getData()));
        siWangYXZM.setSiWangRQ(CDAUtil.toD15(vo.getSIWANGRQ()));

        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        if (!StringUtil.isNullOrEmpty(vo.getSIWANGDDDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV02_01_103.getCode(), new HashSet<>(Collections.singletonList(Converter.toString(vo.getSIWANGDDDM()))));
            Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));
            String value = ZhiJiUtils.getValue(HealthDictionaryEnum.CV02_01_103.getCode(), Converter.toString(vo.getSIWANGDDDM()), valueByCode);
            siWangYXZM.setSiWangDD(new CodeObject(vo.getSIWANGDDDM(), value));
        }


        siWangYXZM.setSiWangDDMC(vo.getSIWANGDDMC());
        siWangYXZM.setSiWangYYList(getSiWangYYList(vo));
        siWangYXZM.setQiTaJBZDList(qiTaZhenDuanList(vo));

        siWangYXZM.setZuiGaoZDDWDM(vo.getZUIGAOZDDWDM());
        siWangYXZM.setZuiGaoZDDWMC(vo.getZUIGAOZDDWMC());
        siWangYXZM.setZuiGaoZDYJ(new CodeObject(vo.getZUIGAOZDYJDM(), vo.getZUIGAOZDYJMC()));
        siWangYXZM.setGenBenSY(new CodeObject(vo.getGENBENSYDM(), vo.getGENBENSYMC()));
        siWangYXZM.setParticipant(createParticipant(vo));

        return cdaCreator.create(siWangYXZM, FTL_PATH);
    }

    private void tranData(SiWangYXZMVo vo) {
        vo.setGERENDABS(toLength17(vo.getGERENDABS()));
        HospitalDTO hospitalDTO = hospitalAPI.info(vo.getYEWUJGDM()).getData();
//        HospitalDTO hospitalDTO = dataMigrationAPI.getByHospitalId(vo.getYEWUJGDM()).getData();
        vo.setYEWUJGDM(hospitalDTO.getOutId());
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(hospital.getOutId());
        custodian.setName(hospital.getName());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    JiaRen createParticipant(SiWangYXZMVo participant) {
        JiaRen custodian = new JiaRen();
        custodian.setName(StringUtil.isNullOrEmpty(participant.getJIASHUXM()) ? "-" : participant.getJIASHUXM());
        custodian.setTelecom(StringUtil.isNullOrEmpty(participant.getJIASHULXDH()) ? "00000000000" : participant.getJIASHULXDH());
        return custodian;
    }

    Author createAuthor(SiWangYXZMVo info) {
        Author author = new Author();
        author.setId(info.getXINGMING());
        author.setTime(CDAUtil.toD15(info.getLURURQ()));
        author.setName(info.getLURURXM());
        HospitalDTO hospitalDTO = hospitalAPI.info(info.getYILIAODWDM()).getData();
//        HospitalDTO hospitalDTO = dataMigrationAPI.getByHospitalId(info.getYILIAODWDM()).getData();
        author.setOrganizationId(hospitalDTO.getOutId());
        author.setOrganizationName(info.getYILIAODWMC());
//        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    PatientRole createPatientRole(SiWangYXZMVo info) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(info.getGERENDABS());
        patientRole.setZhuYuanHBS("-");
        patientRole.setPatient(createPatient(info));

        Addr addr = new Addr();
        addr.setState(info.getJUZHUDZSFMC());
        addr.setCity(info.getJUZHUDZSMC());
        addr.setCounty(info.getJUZHUDZXMC());
        addr.setTownship(info.getJUZHUDZXZMC());
        addr.setStreetName(info.getJUZHUDZSQMC());
        addr.setHouseNumber(info.getJUZHUXXDZ());
        patientRole.setAddr(addr);
        return patientRole;
    }

    Patient createPatient(SiWangYXZMVo info) {
        Patient patient = new Patient();
        patient.setName(info.getXINGMING());
        patient.setId(info.getZHENGJIANHM());
        if (!StringUtil.isNullOrEmpty(info.getXINGBIEDM())) {
            patient.setAdministrativeGender(new CodeObject(info.getXINGBIEDM(), info.getXINGBIEMC()));
        }
        if (info.getCHUSHENGRQ() != null) {
            patient.setBirthTime(CDAUtil.toD8(info.getCHUSHENGRQ()));
        }
        patient.setMaritalStatus(new CodeObject(info.getHUNYINZKDM(), info.getHUNYINZKMC()));
        patient.setEmployerOrganization(info.getGONGZUODW());
        patient.setTelecom(info.getDANWEIDH());

        Addr addr = new Addr();
        addr.setState(info.getHUKOUDZSFMC());
        addr.setCity(info.getHUKOUDZSMC());
        addr.setCounty(info.getHUKOUDZXMC());
        addr.setTownship(info.getHUKOUDZXZMC());
        addr.setStreetName(info.getHUKOUDZSQMC());
        patient.setHouseholdAddr(addr);

        //todo 获取学历名称
        if (!StringUtil.isNullOrEmpty(info.getXUELIDM())) {
            patient.setEducationLevel(new CodeObject(info.getXUELIDM(), info.getWENHUACDMC()));
        }
        if (!StringUtil.isNullOrEmpty(info.getZHIYELBDM())) {
            patient.setOccupation(new CodeObject(info.getZHIYELBDM(), info.getZHIYEMC()));
        }
        return patient;
    }

    List<SiWangYY> getSiWangYYList(SiWangYXZMVo info) {
        List<SiWangYY> list = new ArrayList<>();
        if (!StringUtil.isNullOrEmpty(info.getZHIJIEDZSWDJBHQKADM())) {
            SiWangYY yy1 = new SiWangYY();
            yy1.setCodeObject(new CodeObject(info.getZHIJIEDZSWDJBHQKADM(), info.getZHIJIEDZSWDJBHQKAMC()));
            list.add(yy1);
        }
        if (!StringUtil.isNullOrEmpty(info.getZHIJIEDZSWDJBHQKBDM())) {
            SiWangYY yy1 = new SiWangYY();
            yy1.setCodeObject(new CodeObject(info.getZHIJIEDZSWDJBHQKBDM(), info.getZHIJIEDZSWDJBHQKBMC()));
            list.add(yy1);
        }
        if (!StringUtil.isNullOrEmpty(info.getZHIJIEDZSWDJBHQKCDM())) {
            SiWangYY yy1 = new SiWangYY();
            yy1.setCodeObject(new CodeObject(info.getZHIJIEDZSWDJBHQKCDM(), info.getZHIJIEDZSWDJBHQKCMC()));
            list.add(yy1);
        }
        if (!StringUtil.isNullOrEmpty(info.getZHIJIEDZSWDJBHQKDDM())) {
            SiWangYY yy1 = new SiWangYY();
            yy1.setCodeObject(new CodeObject(info.getZHIJIEDZSWDJBHQKDDM(), info.getZHIJIEDZSWDJBHQKDMC()));
            list.add(yy1);
        }
        return list;
    }

    private List<CodeObject> qiTaZhenDuanList(SiWangYXZMVo info) {
        List<CodeObject> list = new ArrayList<>();
        if (!StringUtil.isNullOrEmpty(info.getQITAJBZD1DM())) {
            list.add(new CodeObject(info.getQITAJBZD1DM(), info.getQITAJBZD1MC()));
        }
        if (!StringUtil.isNullOrEmpty(info.getQITAJBZD2DM())) {
            list.add(new CodeObject(info.getQITAJBZD2DM(), info.getQITAJBZD2MC()));
        }
        if (!StringUtil.isNullOrEmpty(info.getQITAJBZD3DM())) {
            list.add(new CodeObject(info.getQITAJBZD3DM(), info.getQITAJBZD3MC()));
        }
        if (StringUtil.isNullOrEmpty(info.getQITAJBZD1DM())
                && StringUtil.isNullOrEmpty(info.getQITAJBZD2DM())
                && StringUtil.isNullOrEmpty(info.getQITAJBZD3DM())) {
            list.add(new CodeObject(info.getZHIJIEDZSWDJBHQKADM(), info.getZHIJIEDZSWDJBHQKAMC()));
        }
        return list;
    }

    private String toLength17(String gerendabs) {
        if (gerendabs.length() < 17) {
            return StringUtil.padRight(gerendabs, 17, '0');
        }
        return gerendabs.substring(0, 17);
    }
}
